package io.jee.medusa.region;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegionUtils {
	
	public static Region getRegion(int id){
		return RegionHelper.getInstance().getRegion(id);
	}
	
	public static List<Region> getRegionParent(){
		return RegionHelper.getInstance().getRegionParent();
	}
	
	public static void main(String[] args) throws IOException, URISyntaxException {
		List<Region> parent = getRegionParent();
		for (Region region : parent) {
			System.out.println(region.getName()+"("+region.getId()+")");
			for (Region region2 : region.getChildren()) {
				System.out.println("   "+region2.getName()+"("+region2.getId()+")");
				for (Region region3 : region2.getChildren()) {
					System.out.println("      "+region3.getName()+"("+region3.getId()+")");
				}
			}
		}
		System.out.println(getRegion(130107).getName());
		RegionHelper.DATA = new ArrayList<>();
		InputStreamReader read = new InputStreamReader(RegionUtils.class.getResourceAsStream("/region.origin"));
		BufferedReader reader = new BufferedReader(read);
		String temp = null;
		Region region = null;
		Region region2 = null;
		Region region3;
		while ((temp = reader.readLine())!=null) {
			if(!temp.equals("")){
				if(Pattern.matches("^\\d+[ ]+[　]{1}[\u4E00-\u9FA5]+$", temp)){
					String sps[] = new RegionUtils().sp(temp);
					region = new Region();
					region.setId(Integer.parseInt(sps[0]));
					region.setName(sps[1]);
					region.setChildren(new ArrayList<>());
					RegionHelper.DATA.add(region);
				}else if(Pattern.matches("^\\d+[ ]+[　]{2}[\u4E00-\u9FA5]+$", temp)){
					String sps[] = new RegionUtils().sp(temp);
					region2 = new Region();
					region2.setId(Integer.parseInt(sps[0]));
					region2.setName(sps[1]);
					region2.setParent(region);
					region2.setChildren(new ArrayList<>());
					region.getChildren().add(region2);
					RegionHelper.DATA.add(region2);
				}else if(Pattern.matches("^\\d+[ ]+[　]{3}[\u4E00-\u9FA5]+$", temp)){
					String sps[] = new RegionUtils().sp(temp);
					region3 = new Region();
					region3.setId(Integer.parseInt(sps[0]));
					region3.setName(sps[1]);
					region3.setParent(region2);
					region2.getChildren().add(region3);
					RegionHelper.DATA.add(region3);
				}else{
					System.out.println("没匹配到："+temp);
				}
			}
		}
		reader.close();
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("d:/region.data")));
		oos.writeObject(RegionHelper.DATA);
		System.out.println(RegionHelper.DATA.size());
		oos.flush();
		oos.close();
	}
	
	private String[] sp(String temp){
		String[] result = null;
		Pattern pattern = Pattern.compile("(\\d+)[ ]+[　]+([\u4E00-\u9FA5]+)");
		Matcher matcher = pattern.matcher(temp);
		if (matcher.find()) {
			result = new String[2];
			result[0] = matcher.group(1);result[1] = matcher.group(2);
			System.out.println(result[0]);
			System.out.println(Integer.parseInt(result[0]));
		}
		return result;
	}

}
